import { hasProperties, uuid, formatCode } from '@/utils/utils';
import { treeBus } from '@/views/bus/treeData';
import { treeType } from '@/types/project';
import { reactive } from 'vue';
import { previewUI } from '@/api/service/project';
import { projectMode } from '@/data/config';


/**
 * todo:整个项目的设置
 *  1.选择模板
 *  2.复制
 */
export const pageSetting = reactive({
    /* true:配置模式 */
    /* false:模板模式 */
    mode: false,
    /* 复制的信息 */
    copyInfo: undefined as any
})


/**
 * todo:设置复制的信息
 *  */
export const setCopyInfo = (obj: treeType) => {
    var newObject = JSON.parse(formatCode(obj))
    newObject.id = uuid()
    pageSetting.copyInfo = newObject
}
/**
 * todo:粘贴复制的信息
 *  */
export const pasteCopyInfo = () => {
    var info = pageSetting.copyInfo as treeType
    if (!info) return
    if (treeBus.selectItem.type === 'folder') {
        info.id = uuid()
        treeBus.selectItem.children.push(info)
    }
}
/* 设为模板模式 */
export const setTemplateMode = () => {
    pageSetting.mode = false
}
/* 设为配置模式 */
export const setConfigMode = () => {
    pageSetting.mode = true
}
var getUUID = () => {
    return uuid().slice(0, 5);
};
/* 预览属性 */
export const previewConfig = reactive({
    code: getUUID(),
    showResult: false,
    image: "",
    url: ""
})
/* 通用预览方法 */
export const preview = (data: treeType) => {
    var { code } = previewConfig;
    previewUI({
        code: code,
        sourceData: data,
        viewMode: projectMode.viewMode,
        editMode: projectMode.editMode
    }).then((res: any) => {
        previewConfig.showResult = true;
        previewConfig.image = res.data;
        previewConfig.url = res.url;
        previewConfig.code = getUUID();
    });
}